import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/common/common_widget.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/mine/model/messagemodel.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'mine_follow.dart' show SelectAllfootWidget;
import 'mine_preview_page.dart';

class MineMessage extends StatefulWidget {
  @override
  _MineMessageState createState() => _MineMessageState();
}

class _MineMessageState extends State<MineMessage> {

  bool offstage = false;
  bool selectall = false;
  List<MineMessageModel> list = [];
  List<MineMessageModel> removeList = [];

  @override
  void initState() {
    super.initState();

    _requestData();
  }

  _requestData() {
    Networktool.request(API.message, method: NETWORKHTTPMethod.GET, success: (data){
      if(data != null) {
        Autogenerated d = Autogenerated.fromJson(data);
        list = d.data;  
        if(mounted) setState(() {});
      }
    });
  }

  _requstDel() {
    selectall = false;
    setState(() {
      offstage = !offstage;
    });
    if(removeList.isEmpty) return;
    Networktool.request(API.message, params: {"mids": removeList.map((e) => e.id).join(",")}, method: NETWORKHTTPMethod.DELETE, success: (data){
      setState(() {
        removeList.forEach((element) { 
          list.remove(element);
        });
        removeList.clear();
      });
    }
    ,fail: (msg) => AlertTool.showToast(msg)
    ,finaly: () => removeList.clear()
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("弹幕管理"),
        centerTitle: true,
        leading: IconButton(
          onPressed: (){
            Navigator.of(context).pop();
          },
          icon: Image.asset("assets/fanhui.png"),
        ),
        actions: [
          IconButton(
            icon: Image.asset("assets/lajit.png"),
            onPressed: _requstDel,
          ),
        ],
      ),
      body: SafeArea(
        bottom: offstage,
        child: Column(
          children: [
            Expanded(
              child: ListView.builder(
                itemCount: list.length,
                itemBuilder: (context, index){
                return InkWell(
                  onTap: (){
                    MineMessageModel temp = list[index];
                    setState(() {
                      if(removeList.contains(temp)) {
                        removeList.remove(temp);
                      } else {
                        removeList.add(list[index]);
                      }
                    });
                  },
                  child: MineMessageCell(offstage: offstage, isselect: removeList.contains(list[index]), model: list[index],)
                );
              }),
            ),
            SelectAllfootWidget(
              offstage: !offstage,
              isselect: selectall,
              isenable: removeList.length != 0,
              submitBtnTitle: "删除",
              onTap: _requstDel
            )
          ],
        ),
      ),
    );
  }
}

class MineMessageCell extends StatefulWidget {

  final MineMessageModel model;
  final bool offstage;
  bool isselect = false;

  MineMessageCell({
    Key key,
    this.offstage,
    this.isselect = false,
    this.model
  }) : super(key: key);

  @override
  _MineMessageCellState createState() => _MineMessageCellState();
}

class _MineMessageCellState extends State<MineMessageCell> {

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Container(
          padding: const EdgeInsets.only(left: 20, top: 10, right: 0),
          child: Column(
            children: [
              Row(
                children: [
                  InkWell(
                    onTap: (){
                       push(context, MinePreviewPage(mobile: widget.model.mobile));
                    },
                    child: CustomCircleAvatar(size: 46.h, url: widget.model.avatar,)
                  ),
                  SizedBox(width: 10),
                  Text(widget.model.username ?? widget.model.mobile, style: instanceTextStyle(14, APP_font_black)),
                  SizedBox(width: 25,),
                  Expanded(
                    child: Container(
                      child: Text(
                        widget.model.message,
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: instanceTextStyle(17, APP_font_black),
                      ),
                    ),
                  ),
                  
                  widget.offstage ? SelectIconWidget(
                      margin: const EdgeInsets.only(right: 15,left: 15),
                      isselect: widget.isselect,
                    ) : SizedBox(width: 25,)
                ],
              ),
              SizedBox(height: 9,),
            ],
          ),
        ),
    );
  }
}